package ru.padre386.geo_utils.data_loaders.common;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: indrdb
 * Date: 07.02.2011
 * Time: 18:48:30
 */
public class PointsIterator {
    public void calc(ITrackContainer container, Integer trackNumber, IFileIteratorCallback... callbacks) {
        if (callbacks == null) {
            return;
        }
        for (IFileIteratorCallback callback : callbacks) {
            callback.onStart();
        }

        if (trackNumber == null) {
            for (IGeoTrack track : container.geoTracks()) {
                for (IFileIteratorCallback callback : callbacks) {
                    callback.onTrack(track);
                }
                iterTrack(track, callbacks);

            }
        } else {
            iterTrack(container.geoTracks().get(trackNumber), callbacks);
        }

        for (IFileIteratorCallback callback : callbacks) {
            callback.onFinish();
        }

    }

    private void iterTrack(IGeoTrack track, IFileIteratorCallback[] callbacks) {
        List<IGeoPoint> points = track.getPoints();
        for (IGeoPoint point : points) {
            for (IFileIteratorCallback callback : callbacks) {
                callback.onPoint(point);
            }
        }
    }


}
